import { selfBuiltList } from "@/services/aigcUser";
import { adminList } from "@/services/userProfile";
import { workOrderAllocate, workOrderTransfer } from "@/services/workOrder";
import UserSession from "@/UserSession";
import { Form, message, Modal, Select,InputNumber } from "antd";
import React from "react";
import { formatMessage } from 'umi-plugin-react/locale';

class AllocateModal extends React.Component{
  constructor(props){
    super(props);
    this.state = {
      visible:false,
      userList:[],
      id:null
    }
  }

  componentDidMount(){
    if(this.props.onRef){
      this.props.onRef(this)
    }
  }

  initData = (id) => {
    this.setState({
      visible:true,
      id
    },() => {
      this.getUserList()
    })
  }

  getUserList = async () => {
    let userList = []
    if(UserSession.getCurrentUser().isAdmin){
      //甲方管理员
      const res = await adminList({current:1,size:999999,applicationId:UserSession.getCurrentUser().applicationId})
      userList =res.records
    }else{
      //乙方
      const res = await selfBuiltList({current:1,size:999999,categoryId:"",name:""})
      userList =res.records
    }
    
    this.setState({userList})
  }

  doCancel = () => {
    this.props.form.resetFields()
    this.setState({visible:false,id:null})
  }

  doValidate = () => {
    this.props.form.validateFields((error,values) => {
      if(!error){
        this.doOk(values)
      }
    })
  }

  doOk = async values => {
    const { id } = this.state
    let res = await workOrderAllocate({id,...values})
    message.info(res.message)
    this.doCancel()
    this.props.allocateOk()
  }

  render(){
    const { visible,userList } = this.state;
    const { form:{getFieldDecorator,getFieldValue} } = this.props
    const formItemLayout = {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 6 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 18 },
      },
    };
    return(
      <Modal
        visible={visible}
        onCancel={this.doCancel}
        onOk={this.doValidate}
        title={formatMessage({ id: 'workOrderProcessing.allocateModal.title' })}
      >
        <Form {...formItemLayout}>
          <Form.Item label={formatMessage({ id: 'workOrderProcessing.allocateModal.handler' })}>
            {getFieldDecorator("type",{rules:[{required:true,message:formatMessage({ id: 'workOrderProcessing.allocateModal.selectMethod' })}]})(
              <Select>
                <Select.Option value={0}>{formatMessage({ id: 'workOrderProcessing.allocateModal.enterprise' })}</Select.Option>
                <Select.Option value={1}>{formatMessage({ id: 'workOrderProcessing.allocateModal.serviceProvider' })}</Select.Option>
              </Select>
            )}
          </Form.Item>
          {
            UserSession.getCurrentUser().isAdmin && getFieldValue("type") == 0 || !UserSession.getCurrentUser().isAdmin && getFieldValue("type") == 1 ? (
              <Form.Item label={formatMessage({ id: 'workOrderProcessing.allocateModal.handlerPerson' })}>
                {
                  getFieldDecorator("targetUserId",{rules:[{required:true,message:formatMessage({ id: 'workOrderProcessing.allocateModal.required' })}]})(
                    <Select placeholder={formatMessage({ id: 'workOrderProcessing.allocateModal.selectPerson' })}>
                      {
                        userList.map(item => {
                          return (
                            <Select.Option value={item.id} key={item.id}>{item.name || item.userName}</Select.Option>
                          )
                        })
                      }
                    </Select>
                  )
                }
              </Form.Item>
            ) : ''
          }
          <Form.Item label={formatMessage({ id: 'workOrderProcessing.allocateModal.processingHours' })}>
            {
              getFieldDecorator('solutionDuration',{initialValue:0.5,rules:[{required:true,message:formatMessage({ id: 'workOrderProcessing.allocateModal.pleaseInput' })}]})(
                <InputNumber min={0} precision={1} step={0.5} />
              )
            }
          </Form.Item>
          
        </Form>
      
      </Modal>
    )
  }
}

export default Form.create()(AllocateModal)